import {getValidDesc as getValidDesc2} from '@/views/product/list/util.js';
import dayjs from 'dayjs'
import {round} from '@/api/util.js';

export function getValidDesc({validType, activeTime, expireTime, validDate, validDateUnit}) {
  if (validType === '40') {
    return '永久有效';
  }
  if (!activeTime) {
    return getValidDesc2({validType, validDate, validDateUnit});
  }
  return `${activeTime || '?'} 至 ${expireTime || '?'}`;
}

export function getValidDescDate({validType, activeTime, expireTime, validDate, validDateUnit}) {
  if (validType === '40') {
    return '永久有效';
  }
  if (!activeTime) {
    return getValidDesc2({validType, validDate, validDateUnit});
  }
  const Time1 = dayjs(activeTime).format('YYYY-MM-DD')
  const Time2 = dayjs(expireTime).format('YYYY-MM-DD')
  return `${Time1 || '?'} 至 ${Time2 || '?'}`;
}

export function getResidueDesc({cardSubType, remainQty, typeCode, cardConsumeDetailsList, occupyQty}) {
  if (cardSubType === '202' || typeCode === '202') {
    return `<span class="remain">不限次数</span>`;
  }
  return `<span class="d-regular remain">${remainQty || 0}</span>&nbsp;/&nbsp;<span class="d-regular">${occupyQty}</span>&nbsp;次`;

  // return `<span class="d-regular remain">${getResidueDescList(cardConsumeDetailsList, remainQty)}</span>&nbsp;/&nbsp;<span class="d-regular">${remainQty || 0}</span>&nbsp;次`;
}

function getResidueDescList(item, remainQty) {
  let num = 0;
  // acc累计器/obj当前元素
  num = item?.reduce((acc, obj) => acc + obj.usedQty, 0) || 0;
  return num + remainQty
}

export function getCardOriginalDesc(item) {
  if (item.cardSubType === '101') {
    return '';
  } else if (item.cardSubType === '202') {
    return `<span class="remain">不限次数</span>`;
  } else {
    return `<span class="d-regular">${item.remainQty || 0}</span>&nbsp;次`;
  }
}

export function getProductDiscount(item) {
  if (!item) {
    return '';
  }

  const {discountGoodsValue, discountServiceValue, discountTimesValue} = item;
  return `居家产品 ${discountGoodsValue ?? '?'}折 | 服务疗程 ${discountServiceValue ?? '?'}折 | 次卡 ${discountTimesValue ?? '?'}折`;
}

export function getResidueAmountDesc({remainAmount, remainBounsScore}) {
  return `<span class="d-regular remain">${round((remainAmount ?? 0) + parseFloat(remainBounsScore ?? 0))?.toFixed(2)}</span>&nbsp;元`;
}

// export function getResidueAmountDesc({returnMoneyCardPrice}) {
//   return `<span class="d-regular remain">${returnMoneyCardPrice}</span>&nbsp;元`;
// }

// 次卡是否可以延期
export function getTimesCardCanPostpone(item) {
  const cardSubType = item.cardSubType;
  const benefitQty =
    item.memberBenefitsBeanList?.reduce((acc, d) => acc + (d.benefitsQty || 0), 0) ?? 0;
  if (cardSubType === '201') {
    // 有限次卡
    return item.remainQty > 0 || benefitQty > 0;
  } else if (cardSubType === '202') {
    // 无限次卡
    return true;
  } else if (cardSubType === '203') {
    // 通卡
    return item.remainQty > 0 || benefitQty > 0;
  } else if (cardSubType === '101') {
    return (item.remainAmount ?? 0) + parseFloat(item.remainBounsScore ?? 0) > 0 || benefitQty > 0;
  }

  return false;
}
